//
//  RangeSlider.m
//  RangeSlider
//
//  Created by Mal Curtis on 5/08/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SingleRangeSlider.h"

@interface SingleRangeSlider (PrivateMethods)
-(float)xForValue:(float)value;
-(float)valueForX:(float)x;
-(void)updateTrackHighlight;
@end

@implementation SingleRangeSlider

@synthesize maximumValue, selectedMaximumValue;

- (id)initWithFrame:(CGRect)frame
{
    CGRect rect = [UIScreen mainScreen].bounds;
    CGFloat top = 10; // 顶端盖高度
    CGFloat bottom = 10 ; // 底端盖高度
    CGFloat left = 10; // 左端盖宽度
    CGFloat right = 10; // 右端盖宽度
    UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
    self = [super initWithFrame:frame];
    if (self) {
        _maxThumbOn = false;
        _padding = 20;
        
        UIImage *imageTrackBg = [UIImage imageNamed:@"history_progress_bg.png"];
        imageTrackBg = [imageTrackBg resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
        _trackBackground = [[UIImageView alloc] initWithImage:imageTrackBg];
        [_trackBackground setFrame:CGRectMake(20, 0, rect.size.width-20, _trackBackground.frame.size.height)];
        _trackBackground.center = self.center;
        [self addSubview:_trackBackground];
        
        UIImage *imageTrack = [UIImage imageNamed:@"history_progress.png"];
        imageTrack = [imageTrack resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
        _track = [[UIImageView alloc] initWithImage:imageTrack];
        [_track setFrame:CGRectMake(20, 0, rect.size.width-20, _track.frame.size.height)];
        
        _track.center = self.center;
        [self addSubview:_track];
        
        _maxThumb = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"security_set"] highlightedImage:[UIImage imageNamed:@"security_set"]];
        _maxThumb.frame = CGRectMake(0,0, self.frame.size.height,self.frame.size.height);
        _maxThumb.contentMode = UIViewContentModeCenter;
        [self addSubview:_maxThumb];
    }
    
    return self;
}


-(void)layoutSubviews
{
    // Set the initial state
    _maxThumb.center = CGPointMake([self xForValue:selectedMaximumValue], self.center.y);
    [self updateTrackHighlight];
    
    
}

-(float)xForValue:(float)value{
    return (self.frame.size.width-(_padding*2))*((value - 200) / (maximumValue - 200))+_padding;
}

-(float) valueForX:(float)x{
    return 200 + (x-_padding) / (self.frame.size.width-(_padding*2)) * (maximumValue - 200);
}

-(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    if(!_maxThumbOn){
        return YES;
    }
    
    CGPoint touchPoint = [touch locationInView:self];
    if(_maxThumbOn){
        _maxThumb.center = CGPointMake(MIN([self xForValue:maximumValue], MAX(touchPoint.x - distanceFromCenter, [self xForValue:200 + 0])), _maxThumb.center.y);
        selectedMaximumValue = [self valueForX:_maxThumb.center.x];
    }
    [self updateTrackHighlight];
    [self setNeedsLayout];
    
    [self sendActionsForControlEvents:UIControlEventValueChanged];
    return YES;
}

-(BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint touchPoint = [touch locationInView:self];
    if(CGRectContainsPoint(_maxThumb.frame, touchPoint)){
        _maxThumbOn = true;
        distanceFromCenter = touchPoint.x - _maxThumb.center.x;
        
    }
    return YES;
}

-(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    _maxThumbOn = false;
}

-(void)updateTrackHighlight{
	_track.frame = CGRectMake(
                              10,
                              _track.center.y - (_track.frame.size.height/2),
                              _maxThumb.center.x - 0,
                              _track.frame.size.height
                              );
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
